package com.ambulance.project.biz.controller.pad;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.ambulance.common.exception.CustomException;
import com.ambulance.common.utils.SecurityUtils;
import com.ambulance.framework.security.service.SysLoginService;
import com.ambulance.framework.task.HdTask;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.domain.AjaxResultData;
import com.ambulance.project.biz.dto.DispatchDto;
import com.ambulance.project.biz.dto.pad.PadUserInfo;
import com.ambulance.project.biz.dto.pad.PatientInfo;
import com.ambulance.project.biz.dto.web.WebCarDto;
import com.ambulance.project.biz.entity.Dispatch;
import com.ambulance.project.biz.entity.Doctor;
import com.ambulance.project.biz.entity.Nurse;
import com.ambulance.project.biz.entity.Patient;
import com.ambulance.project.biz.enums.Source;
import com.ambulance.project.biz.service.*;
import com.ambulance.project.biz.util.ALiSms;
import com.ambulance.project.manager.utils.DoctorAndNurseCommonUtil;
import com.ambulance.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 人员信息 前端控制器
 * </p>
 *
 * @author zr
 * @since 2020-04-21
 */
@Api(tags = "随车医生-登陆")
@RestController
@RequestMapping(value = "/pad/v1/user", method = RequestMethod.POST)
public class PadUserController {

    @Autowired
    private PatientService patientService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private DoctorAndNurseCommonUtil padCommonUtil;
    @Autowired
    private WebCustomerService webCustomerService;
    @Autowired
    private MyCustomerService myCustomerService;

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    private DoctorService doctorService;
    @Autowired
    private NurseService nurseService;


    /**
     * 随车医生-登陆
     *
     * @return
     */
    @ApiOperation(value = "登陆", notes = "登陆", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/login")
    public Object login(@RequestParam(value = "phone") String phone,
                        @RequestParam(value = "password") String password
    ) throws IOException {
        String token = sysLoginService.loginNoCaptcha(phone, password, Source.PAD);
        return AjaxResult.success(token);
    }


    @ApiOperation(value = "获取验证码", notes = "获取验证码", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/getcode")
    public Object getCode(@RequestParam(value = "phone") String phone
    ) {
        String code = "123456";
//        String code = Integer.toString((int)((Math.random() * 9 + 1) * 100000));
//        SendSmsResponse res = ALiSms.sendSms(phone, "SMS_194026430", code);
//        if (res.getCode().equals("ok")) {
            redisTemplate.opsForValue().set("5G_pad_code" + phone,code,5, TimeUnit.MINUTES);
            return AjaxResult.success("success");
//        }
//        return AjaxResult.error("error");
    }

    @ApiOperation(value = "急救绿通-出诊打卡", notes = "急救绿通-出诊打卡", produces = "application/json")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = DispatchDto.class, message = "响应")
    })
    @RequestMapping(value = "/index")
    public Object index() {

        Dispatch one = padCommonUtil.getDispatch();
        if (CommonUtils.checkNull(one)) {
            return new CustomException("调度信息不存在");
        }
        //随车医生登陆后显示信息
        DispatchDto dispatchDto = DispatchDto.copyFromBean(one);
        return AjaxResult.success(dispatchDto);
    }

    /**
     * 随车医生-患者信息
     *
     * @param
     * @return
     */
    @ApiOperation(value = "急救绿通-患者档案", notes = "急救绿通-患者档案", produces = "application/json")
    @ApiImplicitParams({

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = PatientInfo.class, message = "响应")
    })
    @RequestMapping(value = "/patientInfo")
    public Object patientInfo(
    ) {
        Dispatch dispatch = padCommonUtil.getDispatch();
        //随车医生登陆后显示信息
        Patient patient = patientService.getById(dispatch.getPatientID());
        PatientInfo info = PatientInfo.fromPatient(patient);
        info.setCarID(dispatch.getCarID());
        info.setCarVin(dispatch.getVin());
        return AjaxResult.success(info);
    }

    /**
     * 随车医生-车辆GPS
     *
     * @return
     */
    @ApiOperation(value = "急救绿通-车辆信息", notes = "急救绿通-车辆信息", produces = "application/json")
    @ApiImplicitParams({

    })
    @ApiResponses({
            @ApiResponse(code = 200, response = WebCarDto.class, message = "响应")
    })
    @RequestMapping(value = "/carGps")
    public Object carGps(
    ) {

        Dispatch dispatch = padCommonUtil.getDispatch();
        WebCarDto webCarDto = myCustomerService.getWebCarDto(dispatch.getCarID());
        return AjaxResult.success(webCarDto);
    }

    @ApiOperation(value = "登陆用户信息", notes = "个人中心-基础信息展示", produces = "application/json")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = PadUserInfo.class, message = "响应")
    })
    @RequestMapping(value = "/user/info")
    public Object loginUserInfo(
    ) {

        return AjaxResult.success(webCustomerService.getUserInfo());
    }

}
